package com.gxpt.app.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.R;
import com.gxpt.app.bean.getallcategory_bean;
import com.utils.ScreenUtils;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.utils.picasso.PicassoUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_mall_types)
public class Activity_mall_types extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.type_main)
    LinearLayout type_main;

    @ViewInject(R.id.type_sub)
    GridLayout type_sub;

    int pic_view_width = 0;
    int pic_view_hight = 0;

    List<getallcategory_bean.getallcategory> type = null;
    getallcategory_bean toData = null;

    String[]  titles = null;
    String[]  ids = null;

    String TYPES_MAIN_ID;//大分类ID
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initView();
        initData();
    }

    @Override
    public void initView() {
        txt_title.setText(getString(R.string.txt_service_all_types));
        int Sviewwidth  = ScreenUtils.getScreenWidth(context);//
        pic_view_width  = (Sviewwidth-AppUtils.dip2px(context,10*2+60)-Sviewwidth/4)/3;
        pic_view_hight  = pic_view_width;
    }

    @Override
    public void initData() {
        toNet_getcategory();
    }

    @Event(value={R.id.toback})
    private void getEvent(View view){
        switch(view.getId()){

            case R.id.toback:
                closeActivity();
                break;
        }
    }

    private void init_List_main_type(){

        if(type == null)   return;
        if(type.size()<=0) return;

        type_main.removeAllViews();

        for (int a = 0; a < type.size(); a++) {
            type_main.addView(creatListItemView(a));


        }
        //设置第一个默认选中
        RelativeLayout typelayout = (RelativeLayout)type_main.getChildAt(0).findViewById(R.id.typelayout);
        typelayout.setBackgroundColor(getResources().getColor(R.color.white));
    }

    private View creatListItemView(int id) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.item_mall_types_main_list, null);
        TextView list_title = (TextView)convertView.findViewById(R.id.list_title);
        list_title.setText(type.get(id).name);

        TextView list_id = (TextView)convertView.findViewById(R.id.list_id);
        list_id.setText(type.get(id).id);

        convertView.setId(id);
        convertView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                int ViewID = v.getId();

                RelativeLayout typelayout = (RelativeLayout)v.findViewById(R.id.typelayout);
                typelayout.setBackgroundColor(getResources().getColor(R.color.white));

                TextView list_title = (TextView)v.findViewById(R.id.list_title);
                TextView list_id = (TextView)v.findViewById(R.id.list_id);
                for(int a=0;a<type_main.getChildCount();a++){
                    if(type_main.getChildAt(a).getId()!=ViewID){
                        RelativeLayout aa = (RelativeLayout)type_main.getChildAt(a).findViewById(R.id.typelayout);
                        aa.setBackgroundColor(getResources().getColor(R.color.common_fafbfd));
                    }
                }

                TYPES_MAIN_ID = list_id.getText().toString();
                type_sub.removeAllViews();
                type_sub.removeAllViewsInLayout();
                init_List_sub_type(ViewID);
            }
        });
        return convertView;
    }


    private void init_List_sub_type(int index_main){

        if(type.get(index_main) == null)   return;
        if(type.get(index_main).children.size()<=0) return;

        titles = null;
        titles = new String[type.get(index_main).children.size()];
        ids = null;
        ids = new String[type.get(index_main).children.size()];

        for (int a = 0; a < type.get(index_main).children.size(); a++) {
            type_sub.addView(creatListItem_subView(index_main,a));
            titles[a] = type.get(index_main).children.get(a).name;
            ids[a] = type.get(index_main).children.get(a).id;
        }

    }

    private View creatListItem_subView(final int index_main, final int index_sub_children) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.item_mall_types_sub_list, null);
        TextView txt_sub_type = (TextView)convertView.findViewById(R.id.txt_sub_type);
        txt_sub_type.setText(type.get(index_main).children.get(index_sub_children).name);

        ImageView pic_type= (ImageView)convertView.findViewById(R.id.pic_type);
        if(type.get(index_main).children.get(index_sub_children).cover.isEmpty()){
            PicassoUtils.Picasso_displayImage(context,R.mipmap.ic_launcher,pic_view_width,pic_view_hight,pic_type);
        }else{
            PicassoUtils.Picasso_displayImage(context,Constant.IP+"/"+type.get(index_main).children.get(index_sub_children).cover,pic_view_width,pic_view_hight,pic_type);
        }
        convertView.setId(Integer.parseInt(type.get(index_main).children.get(index_sub_children).id));





        convertView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
//            int ViewID = v.getId();
////                main_id   = ""+ViewID;
////                main_name = ""+list_title.getText().toString();
////                toNet_getregion();
                Bundle b = new Bundle();
//                b.putSerializable("TYPES",toData);
                b.putStringArray("data1",titles);
                b.putStringArray("data2",ids);
                b.putInt("TYPES_MAIN_INDEX",index_main);
                b.putInt("TYPES_SUB_INDEX",index_sub_children);
                b.putString("TYPES_MAIN_ID" , TYPES_MAIN_ID);
                b.putString("TYPES_SUB_ID"  , type.get(index_main).children.get(index_sub_children).id);
                jumpActivity(Activity_mall_goods_list.class,b,true);
            }
        });
        return convertView;
    }


    //////NET DO
    StringCallback do_getallcategory = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                getallcategory_bean data = new Gson().fromJson(s, getallcategory_bean.class);
                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    if(data.data != null){
                        if(data.data.size()>0){
                            toData = data;
                            type = data.data;

                                //main
                                init_List_main_type();
                                if(data.data.get(0).children !=null){
                                    if(data.data.get(0).children.size()>0){
                                        //sub
                                        init_List_sub_type(0);
                                    }
                                }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };

    /////NET
    private void toNet_getcategory(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "shop");
            jsonBody.put("serviceName",          "getallcategory");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        HttpUtils.http_post_string(Constant.URL_getallcategory,jsonBody.toString(),do_getallcategory);
    }
}
